/**
 * @Author: easten
 * @Created: 2024-04-15 23:48:14
 * @Description: 终端管理抽屉
 *
 */
import { ApiTerminal } from "_common/api/iot";
import { DrawerProps, TreeDataModel } from "_common/DataType";
import { IotTerminal } from "_common/entity";
import { AxiosInstance } from "axios";
import React, { useEffect, useRef } from "react";
import {
  Button,
  Drawer,
  Form,
  FormInstanceFunctions,
  Input,
} from "tdesign-react";
import FormItem from "tdesign-react/es/form/FormItem";
type TerminalDrawerProps = DrawerProps & {
  gateway?: TreeDataModel;
  axios: AxiosInstance;
};
const TerminalDrawer: React.FC<TerminalDrawerProps> = (
  props: TerminalDrawerProps
) => {
  const { open, axios, edit, onSuccess, value, gateway } = props;
  const formRef = useRef<FormInstanceFunctions>();
  const apiTerminal = new ApiTerminal(axios);
  useEffect(() => {
    return () => {
      formRef.current?.reset();
    };
  }, []);

  useEffect(() => {
    if (value && edit) {
      formRef.current?.setFieldsValue(value);
    }
  }, [edit, value]);

  const handleClose = () => {
    onSuccess?.(false);
    formRef.current?.reset();
  };

  // 新增终端
  const createTerminal = async (data: IotTerminal) => {
    const response = await apiTerminal.createTerminal({
      ...data,
      gatewayId: gateway?.value,
    });
    if (response && response.code == 200) {
      handleClose();
    }
  };
  // 更新终端
  const updateTerminal = async (data: IotTerminal) => {
    const response = await apiTerminal.updateTerminal({
      ...data,
      gatewayId: gateway?.value,
      id: value?.id,
    });
    if (response && response.code == 200) {
      handleClose();
    }
  };

  return (
    <Drawer
      header={`${edit ? "编辑" : "添加"}终端`}
      footer={false}
      visible={open}
      sizeDraggable
      size="medium"
      onClose={handleClose}
    >
      <Form
        ref={formRef}
        labelWidth={100}
        onSubmit={async (data) => {
          // 是否校验通过
          if ((await formRef.current?.validateOnly()) == true) {
            edit
              ? updateTerminal(data.fields as IotTerminal)
              : createTerminal(data.fields as IotTerminal);
          }
        }}
      >
        <FormItem
          label="终端名称"
          rules={[{ required: true }]}
          name="terminalName"
        >
          <Input />
        </FormItem>
        <FormItem
          label="终端编码"
          rules={[{ required: true }]}
          name="terminalCode"
        >
          <Input />
        </FormItem>
        <FormItem
          label="总线地址"
          rules={[
            { required: true },
            {
              // 只能是数字
              pattern: /^\d+$/,
              message: "只能输入数字",
            },
          ]}
          name="address"
        >
          <Input />
        </FormItem>
        <FormItem className="float-right">
          <Button type="submit" theme="primary">
            提交
          </Button>
          <Button type="reset" theme="success" style={{ marginLeft: 12 }}>
            重置
          </Button>
        </FormItem>
      </Form>
    </Drawer>
  );
};
export default TerminalDrawer;
